<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page import="java.util.*,java.lang.reflect.*" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>表单数据查看</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .data-container {
            max-width: 800px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .data-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        .data-table th,
        .data-table td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .data-table th {
            background-color: #f8f9fa;
            font-weight: bold;
            color: #333;
        }

        .data-table tr:hover {
            background-color: #f5f5f5;
        }

        .btn {
            display: inline-block;
            padding: 10px 20px;
            background-color: #4caf50;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            margin-top: 20px;
        }

        .btn:hover {
            background-color: #45a049;
        }

        .no-data {
            text-align: center;
            color: #666;
            padding: 20px;
        }

        .session-info {
            margin-top: 20px;
            padding: 15px;
            background-color: #e9ecef;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div class="data-container">
    <div class="data-header">
        <h1>表单数据查看</h1>
    </div>

    <% Object formData = session.getAttribute("formData");
        if (formData
                != null) {
            Class<?>
                    formDataClass = formData.getClass();
            Method[] methods =
                    formDataClass.getDeclaredMethods(); %>
    <table class="data-table">
        <tr>
            <th>字段</th>
            <th>值</th>
        </tr>
        <% for (Method method : methods) {
            if
            (method.getName().startsWith("get") &&
                    !method.getName().equals("getClass")) {
                String fieldName =
                        method.getName().substring(3, 4).toLowerCase() +
                                method.getName().substring(4);
                Object value = null;
                try {
                    value
                            = method.invoke(formData);
                    if (value instanceof List) {
                        List<?>
                                list = (List<?>) value;
                        value = String.join(", ",
                                list.stream().map(Object::toString).toArray(String[]::new));
                    }
                } catch (Exception e) {
                    value = "[读取出错]";
                } %>
        <tr>
            <td><%= fieldName %>
            </td>
            <td><%= value != null ? value : "" %>
            </td>
        </tr>
        <% }
        } %>
    </table>
    <% } else { %>
    <div class="no-data">
        <p>暂无表单数据</p>
    </div>
    <% } %>

    <div class="session-info">
        <h3>Session 信息</h3>
        <p>Session ID: <%= session.getId() %>
        </p>
        <p>创建时间: <%= new Date(session.getCreationTime()) %>
        </p>
        <p>
            最后访问时间: <%= new Date(session.getLastAccessedTime()) %>
        </p>
        <p>
            最大不活动时间: <%= session.getMaxInactiveInterval() %> 秒
        </p>
    </div>

    <div style="text-align: center; margin-top: 20px">
        <a href="form-demo.jsp" class="btn">返回表单</a>
    </div>
</div>
</body>
</html>
